package com.doge.service.admin.order;

import com.doge.bean.bo.order.OrderListBo;
import com.doge.bean.bo.order.ShipBO;
import com.doge.bean.pojo.order.Order;
import com.doge.bean.pojo.order.OrderExample;
import com.doge.bean.pojo.order.OrderGoods;
import com.doge.bean.pojo.order.OrderGoodsExample;
import com.doge.bean.pojo.user.User;
import com.doge.bean.vo.order.DetailOrderVo;
import com.doge.bean.vo.order.OrderResultVo;
import com.doge.bean.vo.order.UserInfoForDetailOrderVo;
import com.doge.mapper.OrderGoodsMapper;
import com.doge.mapper.OrderMapper;
import com.doge.mapper.UserMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @program: doge
 * @description: 订单服务
 * @author: Mr.Jiang
 * @create: 2021-11-30 21:21
 **/
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderGoodsMapper orderGoodsMapper;
    @Autowired
    UserMapper userMapper;

    @Override
    public OrderResultVo list(OrderListBo orderListBo) {

        PageHelper.startPage(orderListBo.getPage(), orderListBo.getLimit());
        OrderExample orderExample = new OrderExample();
        if(orderListBo.getOrderStatusArray()!=null){
            orderExample.createCriteria().andOrderStatusIn(orderListBo.getOrderStatusArray());
        }
        if(orderListBo.getUserId()!=null){
            orderExample.createCriteria().andUserIdEqualTo(orderListBo.getUserId());
        }
        if(orderListBo.getOrderSn()!=null){
            orderExample.createCriteria().andOrderPriceEqualTo(BigDecimal.valueOf(orderListBo.getOrderSn()));
        }
        orderExample.createCriteria().andDeletedEqualTo(false);
        List<Order> orders = orderMapper.selectByExample(orderExample);
        for (Order order : orders) {
            order.setPayId(null);
            order.setPayTime(null);
            order.setRefundAmount(null);
            order.setRefundType(null);
            order.setRefundContent(null);
            order.setRefundTime(null);
            order.setConfirmTime(null);
            order.setAddTime(null);
        }
        OrderResultVo orderResultVo = new OrderResultVo();
        orderResultVo.setTotal(orders.size());
        orderResultVo.setPages(1);
        orderResultVo.setLimit(orderListBo.getLimit());
        orderResultVo.setPage(1);
        orderResultVo.setList(orders);
        return orderResultVo;
    }

    @Override
    public DetailOrderVo Detail(Integer id) {
        Order order = orderMapper.selectByPrimaryKey(id);
        OrderGoodsExample orderGoodsExample = new OrderGoodsExample();
        orderGoodsExample.createCriteria().andOrderIdEqualTo(order.getId());
        List<OrderGoods> orderGoods = orderGoodsMapper.selectByExample(orderGoodsExample);
        User user = userMapper.selectByPrimaryKey(order.getUserId());
        UserInfoForDetailOrderVo userInfoForDetailOrderVo = new UserInfoForDetailOrderVo();
        userInfoForDetailOrderVo.setUsername(user.getUsername());
        userInfoForDetailOrderVo.setAvatar(user.getAvatar());
        order.setPayId(null);
        order.setPayTime(null);
        order.setShipSn(null);
        order.setShipChannel(null);
        order.setShipTime(null);
        order.setRefundAmount(null);
        order.setRefundType(null);
        order.setRefundContent(null);
        order.setRefundTime(null);
        order.setConfirmTime(null);
        order.setEndTime(null);
        DetailOrderVo detailOrderVo = new DetailOrderVo();
        detailOrderVo.setOrder(order);
        detailOrderVo.setOrderGoods(orderGoods);
        detailOrderVo.setUser(userInfoForDetailOrderVo);
        return detailOrderVo;
    }

    @Override
    public void ship(ShipBO shipInfo, HttpServletRequest request) {
        // 更改 订单状态，添加 发货信息
        Order order = new Order();
        order.setId(shipInfo.getOrderId());
        order.setOrderStatus((short)301); // 表示已经发货
        order.setShipChannel(shipInfo.getShipChannel());
        order.setShipSn(shipInfo.getShipSn());
        order.setShipTime(new Date());
        // 更新给 数据路
        orderMapper.updateByPrimaryKeySelective(order);
        // 根据orderId获取orderSn
        Order orderSn = orderMapper.selectByPrimaryKey(order.getId());
        request.setAttribute("orderSn",orderSn.getOrderSn());
    }

    @Override
    public void delete(Integer orderId, HttpServletRequest request) {
        // 根据orderId获取orderSn
        Order orderSn = orderMapper.selectByPrimaryKey(orderId);
        request.setAttribute("orderSn",orderSn.getOrderSn());
    }
}
